package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.controller.ShopGoodsController;
import cc.linker.steplink.crm.merchant.service.NewMallService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchant.service.integral.IntegralGoodsService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小B商品
 */
@Slf4j
@RestController
@RequestMapping("/xb/integral/goods")
public class XbIntegralGoodsController {

    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private IntegralGoodsService integralGoodsService;

    /**
     * 积分商品列表
     *
     * @param pageIndex 页码
     * @param pageSize  页面大小
     * @param status    状态
     * @param key       关键字
     */
    @GetMapping("/list")
    public Object getGoodsList(@RequestParam(value = "status", defaultValue = "0", required = false) Integer status,
                               @RequestParam(value = "key", defaultValue = "", required = false) String key,
                               @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                               @RequestParam(value = "goodsType", defaultValue = "-1", required = false) Integer goodsType,
                               Long groupId,HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        if(userinfo.getSaler()){
            String customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            params.put("belongId", customerId);
        } else {
            params.put("belongId",orgId);
        }
        if (!StringUtils.isEmpty(key)) {
            try {
                key = URLDecoder.decode(key.replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8");
                key = key.replaceAll("%", "/%");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        params.put("key", key);
        params.put("goodsStatus", status);
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        params.put("goodsType", goodsType);
        params.put("groupId", groupId);
        return integralGoodsService.getGoodsListByAdminId(params);
    }

    /**
     * 商品新增编辑
     */
    @PostMapping("/save")
    public void save(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        if(userinfo.getSaler()){
            String customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            params.put("belongId", customerId);
        } else {
            params.put("belongId",orgId);
        }
        params.put("orgId", orgId);
        params.put("adminId", adminId);
        integralGoodsService.saveByAdminId(params,orgId);
    }


//
//    /**
//     * @param goodsId 商品id
//     * @param mine    商品类型0-自有1-联盟2-共享
//     */
//    @PostMapping("/updateStatus")
//    public void updateStatus(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
//        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
//        String adminId = userinfo.getAdminId();
//        if(null == params.get("param")){
//            throw GlobalException.build("至少选择一个商品");
//        }
//        List<Map<String, Object>> param = (List<Map<String, Object>>)params.get("param");
//        for(Map<String, Object> pa:param){
//            pa.put("adminId", adminId);
//            newMallService.updateStatus(pa);
//        }
//    }
//
//    /**
//     * 判断用户是否可以新增、上架商品
//     */
//    @GetMapping("/judge")
//    public void judge(HttpServletRequest request) throws GlobalException {
//        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
//        Map<String, Object> params = new HashMap<>();
//        String adminId = userinfo.getAdminId();
//        params.put("adminId", adminId);
//        String orgId = userinfo.getOrgId();
//        params.put("orgId", orgId);
//        Boolean ifSales = userinfo.getSaler();
//        if (ifSales) {
//            // 小b
//            newMallService.judgeStateOfAdmin(params);
//        } else {
//            // 大b
//            // 发布不做判断
//            //newMallService.judgeStateOfAdminOrg(params);
//        }
//        // 判断商品创建数量是否到达上限
//        newMallService.judgeGoodsLimitByOrgId(params);
//    }

}
